package com.seal.tsms.other.controller;

import com.seal.tsms.base.controller.BaseController;
import com.seal.tsms.base.service.BaseService;
import com.seal.tsms.base.util.DateUtils;
import com.seal.tsms.other.entity.Token;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by seal on 2016/3/26.
 */
@Controller
@RequestMapping("/other/token")
public class TokenController extends BaseController<Token,String> {

    @Override
    @Autowired
    @Qualifier("tokenServiceImpl")
    public void setSelfService(BaseService<Token, String> selfService) {
        super.setSelfService(selfService);
    }

    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public String register(){
        return "other/register";
    }
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ResponseBody
    public Map register(Token token, HttpSession session){
        Map data=new HashMap();
        try{
            token.setCreateTime(DateUtils.getCurrentDate());
            if(token.getNickName().length()>15){
                throw new Exception("你输入的名字超过15了");
            }
            token.setNickName(HtmlUtils.htmlEscape(token.getNickName()));
            getSelfService().save(token);
            session.setAttribute("token",token);
            data.put("state",true);
            data.put("msg","注册成功");
        }catch (Exception e){
            data.put("state",false);
            data.put("msg","注册失败");
            log.error("注册失败:"+e.getMessage(),e);
        }

       return data;
    }
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "other/login";
    }
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public Map login(Token token, HttpSession session){
        Map data=new HashMap();
        try{
            token.setCreateTime(DateUtils.getCurrentDate());
            DetachedCriteria dc=DetachedCriteria.forClass(Token.class);
            dc.add(Restrictions.eq("telNo",token.getTelNo()));
            token=getSelfService().findOne(dc);
            if(token!=null) {
                session.setAttribute("token", token);
            }else {
                throw new Exception("你没有注册");
            }
            data.put("state",true);
            data.put("msg","登陆成功");
        }catch (Exception e){
            data.put("state",false);
            data.put("msg",e.getMessage());
            log.error("登陆失败",e);
        }

        return data;
    }
    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute("token");
        return "redirect:/other/index.do";
    }

}
